﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace beyCoo4Sharepoint
{
    /// <summary>
    /// Logger class that logs errors and messages from the beyCoo solution to the ULS logs of Sharepoint
    /// </summary>
    public class beyCooLogger : SPDiagnosticsServiceBase
    {
        public static string beyCooDiagnosticAreaName = "beyCoo";
        private static beyCooLogger _Current;
        public static beyCooLogger Current
        {
            get
            {
                if (_Current == null)
                {
                    _Current = new beyCooLogger();
                }

                return _Current;
            }
        }

        private beyCooLogger()
            : base("beyCoo Logging Service", SPFarm.Local)
        {

        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
        {
            new SPDiagnosticsArea(beyCooDiagnosticAreaName, new List<SPDiagnosticsCategory>
            {
                new SPDiagnosticsCategory("beyCooError", TraceSeverity.Unexpected, EventSeverity.Error),
                new SPDiagnosticsCategory("beyCooMessage", TraceSeverity.Verbose, EventSeverity.Error)
            })

        };

            return areas;
        }

        public static void LogError(string categoryName, string errorMessage)
        {
            
            SPDiagnosticsCategory category = beyCooLogger.Current.Areas[beyCooDiagnosticAreaName].Categories[categoryName];
            try
            {
                beyCooLogger.Current.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public static void LogMessage(string categoryName, string message)
        {

            SPDiagnosticsCategory category = beyCooLogger.Current.Areas[beyCooDiagnosticAreaName].Categories[categoryName];
            try
            {
                beyCooLogger.Current.WriteTrace(0, category, TraceSeverity.Medium, message);
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
